﻿/*
	* Project	: SharePoint Dynamic Forms
	* Version	: 1.0
	* Company	: SharePoint Frontier Software Pvt Ltd. (http://www.sharepointfrontier.com)
	* Author	: Ashok Raja .T (Blog : http://www.ashokraja.me)
	* Licence	: Published under Microsoft Public License (Ms-PL)
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using Microsoft.SharePoint.WebControls;

namespace SFS.SharePoint.DynamicForms
{
    public class SPDynamicFieldFieldValidationManger
    {

        StringBuilder sb = new StringBuilder();
        private bool ValidationFlag = true;
        public List<SPDynamicField> Fields { get; set; }
        private SPDynamicFieldFieldValidationManger() { }
        public string ListName { get; set; }
        public SPDynamicFieldFieldValidationManger(string FormId,string FormIdColumn, string FormData, string FormsList, Control ctl, string ControlIdentifier)
        {
            SPDynamicForm Frm = new SPDynamicForm(FormId, FormIdColumn,FormData, FormsList);
            Fields = Frm.GetFormFields();
            ListName = Frm.ListName;
            //Fields.Reverse();
            foreach (SPDynamicField Field in Fields)
            {
                FormField formField = (FormField)ctl.FindControl(ControlIdentifier + Field.InternalName);
                if(formField==null)continue;

                string fieldType=formField.Field.TypeAsString;
                //Field.Value = formField.Value;
                Field.FieldType = fieldType;
                Field.Value = fieldType == "Note" && formField.Value.ToString().ToUpper() == "<DIV></DIV>" ? "" : formField.Value;
                Field.FieldCollection = Fields;
                bool tempFlag = Field.IsValid();
                if (!tempFlag)
                {
                    if (fieldType == "Text" )
                    {
                        TextField fld = formField.Controls[0] as TextField;
                        fld.ErrorMessage = Field.LastError;
                        fld.IsValid = false;
                    }
                    else if (fieldType == "Note")
                    {
                        RichTextField fld = formField.Controls[0] as RichTextField;
                        fld.ErrorMessage = Field.LastError;
                        fld.IsValid = false;
                    }
                    else if (fieldType == "DateTime")
                    {
                        DateTimeField fld = formField.Controls[0] as DateTimeField;
                        fld.ErrorMessage = Field.LastError;
                        fld.IsValid = false;
                    }
                    else if (fieldType == "Number")
                    {
                        NumberField fld = formField.Controls[0] as NumberField;
                        fld.ErrorMessage = Field.LastError;
                        fld.IsValid = false;
                    }
                    else if (fieldType == "Lookup")
                    {
                        LookupField fld = formField.Controls[0] as LookupField;
                        fld.ErrorMessage = Field.LastError;
                        fld.IsValid = false;
                    }
                }
                
                if (!tempFlag)
                    ValidationFlag = tempFlag;
            }
        }

        public bool IsValid()
        {
            return ValidationFlag;
        }

        public string ErrorMessage()
        {
            foreach (SPDynamicField Field in Fields)
            {
                foreach (SPDynamicFieldValidationRule rule in Field.ValidationRules)
                {
                    sb.AppendLine(rule.ValidationMessage + Environment.NewLine);
                }
            }
            return sb.ToString();
        }
    }
}
